package com.app.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.app.mapper.UserMapper;
import com.app.pojo.Company;
import com.app.pojo.User;
import com.app.service.ICompanyService;
import com.app.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 单位信息 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2024-07-29
 */
@Api(tags = "缴费单位相关接口")
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private ICompanyService companyService;
    @Autowired
    private UserMapper userMapper;



    @ApiOperation(value = "查询单位列表")
    @GetMapping("/list")
    public Result<List<Company>> list() {
        return companyService.getList();
    }

    @ApiOperation(value = "查询缴费单位咨询电话")
    @ApiImplicitParams({@ApiImplicitParam(name = "companyName", value = "单位名称", dataTypeClass = String.class)})
    @GetMapping("/getCompanyByCompanyName")
    public Result getCompanyByCompanyName(String companyName, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error( "token不存在");
        }
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken == null) {
            Result result = new Result(401, "登录失效");
            return result;
        }
        String userId = loginIdByToken.toString();
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error( "用户不存在");
        }
        if(!StringUtils.isNotEmpty(companyName)){
            return Result.error("请输入单位名称后查询！");
        }
        Company company = companyService.getCompanyByCompanyName(companyName);
        if (company == null){
            return Result.error("缴费单位不存在！");
        }
        return Result.success(company);
    }





}

